/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.tools;

import java.util.Date;

import org.telosys.common.logger.TelosysClassLogger;

/**
 * Technical thread designed to check all the connections presents in the pool.
 *  
 * @author Laurent GUERIN
 * @since v 1.0.2
 */
public class ConnectionPoolThread extends Thread //implements Runnable
{
    private static final TelosysClassLogger log = new TelosysClassLogger(ConnectionPoolThread.class);

    private long _sleepDuration = 60 ; // Default duration : 60 seconds
        
    /**
     * Constructor
     * @param sleepDuration : the thread's sleep duration between 2 actions
     */
    public ConnectionPoolThread(long sleepDuration)
    {
        super();
        this.setName(this.getClass().getName());
        this._sleepDuration = sleepDuration;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        System.out.println("run ... " + new Date() );
        try
        {
            while ( true ) 
            {
                Thread.sleep( 1000 * _sleepDuration );
                action();
            } 
            
        } catch (InterruptedException e) // thrown by the context listener when it stops
        {
            log.info("Thread interrupted");
            //throw new TelosysRuntimeException("Thread interrupted", e);
        }
    }

    private void action()
    {
        // TODO : implements the action
        System.out.println( this.toString() + "do action - date = " + new Date() );
    }
    
//    public static void main(String[] args)
//    {
//        try
//        {
//            ConnectionPoolThread thread = new ConnectionPoolThread(10);
//            //thread.setDaemon(true);
//            thread.start();
//            System.out.println("Thread " + thread.toString() + " started.");
//        } catch (Throwable t)
//        {
//            System.out.println("Throwable exception : " + t.toString() + " - Message = " + t.getMessage());    
//            t.printStackTrace();
//        }
//    }
    
}
